package myky.com.doctorapp.presenter;

import android.util.Log;

import io.reactivex.disposables.Disposable;
import myky.com.doctorapp.api.ResponseTransformer;
import myky.com.doctorapp.entity.BannerEntity;
import myky.com.doctorapp.entity.CaseCountsEntity;
import myky.com.doctorapp.entity.NewCaseListEntity;
import myky.com.doctorapp.entity.RecDoctorListEntity;
import myky.com.doctorapp.entity.TagListEntity;
import myky.com.doctorapp.api.schedulers.SchedulerHelper;
import myky.com.doctorapp.base.BasePresenterIm;
import myky.com.doctorapp.base.RxSubscribe;
import myky.com.doctorapp.base.SimpleSubscribe;
import myky.com.doctorapp.contract.HomeFragmentContract;
import myky.com.doctorapp.model.HomeFragmentModel;

/**
 * @author Mr.li
 */
public class HomeFragmentPresenter extends BasePresenterIm<HomeFragmentContract.View> implements HomeFragmentContract.Presenter {

    private HomeFragmentModel homeFragmentModel;

    public HomeFragmentPresenter() {
        homeFragmentModel = new HomeFragmentModel();
    }

    @Override
    public void getBanner() {
        homeFragmentModel.getBanner().subscribe(new RxSubscribe<BannerEntity.PayLoadBean>() {
            @Override
            public void onSubscribe(Disposable d) {
                addSubscription(d);
                mView.showContentView();
                mView.showLoadingView();
            }

            @Override
            protected void onSuccess(BannerEntity.PayLoadBean beans) {
                mView.showContentView();
                if (beans.getAdvertList() != null && beans.getAdvertList().size() > 0) {
                    mView.showBannerList(beans.getAdvertList());
                }
            }

            @Override
            protected void onFailed(int code, String msg) {
                mView.logCat(msg);
                mView.showContentView();
//                mView.showRetryView();
            }
        });
    }

    private String TAG = "HomeFragmentPresenter";

    @Override
    public void getTagList(String start, String end) {

        homeFragmentModel.getTagList(start, end)
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerHelper.applySchedulers())
                .subscribe(new SimpleSubscribe<TagListEntity.PayLoadBean>() {
                    @Override
                    protected void onSuccess(TagListEntity.PayLoadBean payLoadBean) {
                        if (payLoadBean.getTagList() != null && payLoadBean.getTagList().size() > 0) {
                            mView.showTagList(payLoadBean.getTagList());
                        }
                    }

                    @Override
                    protected void onFailed(String msg) {
                        mView.showDataError(msg);
                        Log.i(TAG, "获取tagList onFailed: " + msg);
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscription(d);
                    }
                });

    }

    @Override
    public void getRecDoctorList(String version, String size, String current, String publish) {
        homeFragmentModel.getRecDoctorList(version, size, current, publish).subscribe(new RxSubscribe<RecDoctorListEntity.DataBean>() {
            @Override
            protected void onSuccess(RecDoctorListEntity.DataBean positionsBeanList) {
                if (positionsBeanList.getPositions() != null && positionsBeanList.getPositions().size() > 0) {
                    mView.showRecDoctorList(positionsBeanList.getPositions());
                }
            }

            @Override
            protected void onFailed(int code, String msg) {
                mView.showDataError(msg);
                Log.i(TAG, "getRecDoctorList onFailed: " + msg);
            }

            @Override
            public void onSubscribe(Disposable d) {
                addSubscription(d);
            }
        });

    }

    @Override
    public void getCaseCounts() {
        homeFragmentModel.getCaseCounts().subscribe(new RxSubscribe<CaseCountsEntity.PayLoadBean>() {
            @Override
            protected void onSuccess(CaseCountsEntity.PayLoadBean bean) {
                mView.showCaseCounts(bean);
            }

            @Override
            protected void onFailed(int code, String msg) {
                mView.showDataError("病历授权数量获取失败");

                Log.i(TAG, "getCaseCounts onFailed: " + msg);
            }

            @Override
            public void onSubscribe(Disposable d) {
                addSubscription(d);
            }
        });
    }

    @Override
    public void getUserCaseList() {
        homeFragmentModel.getUserCaseList().subscribe(new RxSubscribe<NewCaseListEntity.PayLoadBean>() {
            @Override
            protected void onSuccess(NewCaseListEntity.PayLoadBean bean) {
                mView.showUserCaseList(bean.getUserIllnesses());
            }

            @Override
            protected void onFailed(int code, String msg) {
                mView.showDataError("病历列表获取失败");
                Log.i(TAG, "getUserCaseList onFailed: " + msg);

            }

            @Override
            public void onSubscribe(Disposable d) {
                addSubscription(d);
            }
        });
    }

}
